Send ecommerce events with an API client
Collect user interactions as events in your ecommerce store.
Capturing real-time user interactions as events gives you actionable insights with click and conversion metrics, and they help you increase your customer engagement and conversions. Events are used to activate Algolia features and products like:
- NeuralSearch
- Algolia Recommend
- Personalization
- Dynamic Re-Ranking
- Query Categorization
- Search analytics
- Revenue analytics
- A/B testing.
Enable events collection
Set the clickAnalytics
parameter to true
when making search requests.
This includes the queryID
parameter in the search response,
which is required for linking events and search requests.
Initialize search-insights.js
Install the search-insights
library.
After installing the search-insights
library, initialize the client in your website.
Understand which events to track
To understand which events you should send, think about the different steps a user takes through your website to reach the final goal: the product purchase.
Starting with a search on any of your pages, a user might take the following actions:
- Select a product from the search results to open the product details page.
- Add a product to the shopping cart.
- Buy the product.
Starting with a search on any of your pages, a user might take the following actions:
- Select a product from the search results to open the product details page.
- Add a product to the shopping cart.
- Buy the product.
Starting with a visit to your home page, a user might take the following actions:
- Select a product category to open a category (product listing) page.
- Select a product to open a product details page.
- Add a product to the shopping cart.
- Buy the product.
Starting with a search on an internet search engine, with a click on a recommendation, or any other external referral, a user might take the following actions:
- Click on an (internet) search result, recommendation, or other link to your website.
- Depending on where the user lands on your website, they continue the same path as if they started with your homepage or with a category page.
Search and search results pages
User action | Method name |
---|---|
User clicks search result | clickedObjectIDsAfterSearch |
User adds product to shopping cart | addedToCartObjectIDsAfterSearch |
User clicks categories/filters | clickedFilters |
User views search results | viewedObjectIDs |
Product listing and category pages
User action | Method name |
---|---|
User clicks product | clickedObjectIDsAfterSearch |
User adds product to shopping cart | addedToCartObjectIDsAfterSearch |
User views category page | viewedObjectIDs |
Product details page
User action | Method name |
---|---|
User adds product to cart | addedToCartObjectIDsAfterSearch |
User views product | viewedObjectIDs |
Checkout page
User action | Method name |
---|---|
User buys product | purchasedObjectIDsAfterSearch |
Keep track of query IDs
Conversion events are often triggered outside of the search results page. In order to correctly attribute which query is responsible for a conversion, it is necessary to keep track of the query ID returned when a search is performed so that it can be included in conversion events.
To relate the conversion events back to the search request made on your search results or category pages, you need to keep track of the query IDs across your pages.
Track click events
To send click events with the Insights client, add the following code whenever a user clicks on a product.
If you want to track clicks on search results pages, add the following code whenever a user clicks on a product.
- The
objectID
is included in the search response for each search result as part of thehits
attribute. - The
positions
array inclickedObjectIdsAfterSearch
contains a one-based index of the product hit in the search results.
Track add-to-cart events
Add the following code to all components and pages where users can add products to their shopping cart.
The objectID
is included in the search response for each search result as part of the hits
attribute.
You should store the query ID with other product details when updating a user’s shopping cart. This makes it easy to record query IDs for each item for any following purchase events.
Track purchase events
To record when a user completes a purchase, add the following code:
Use objectData
to record per-object data, such as the price and quantity of the purchased products.
Users often purchase items that were added to the cart in response to different queries. For example, a user might search for “shoes” and add a pair of shoes to their cart. Then, they search for “lamp” and add a lamp to their cart. Then, they check out and complete the purchase of both items. In this case, there would be a single purchase event. Each item---the shoes and the lamp---will have the query ID of the search they originated from.
(Optional) Handle known users for Personalization
For effective personalization, you need to identify users across sessions. It’s best to use an identifier from your authentication, user profile, or ecommerce service after users signed in.
After getting the identifier from your system,
set it as authenticatedUserToken
parameter.
When you set the authenticatedUserToken
parameter in the Insights client, you also need to update
the user token you send with your search requests.
If you can’t get persistent user identifiers from your system, you can store the anonymous user token in a cookie after obtaining user consent.
For more information, see User token.
(Optional) Send view events for Personalization
Personalization benefits from the same click and conversion events, plus it can use view events to enrich user profiles.
To capture view events, such as when a user views search results, add this code: